package com.library.admin.listener;

import books.borrowing.entity.SubscriberEntity;

import com.library.admin.AdmLibrary;
import com.library.admin.form.UpdateSubscriberAccountForm;
import com.library.admin.manager.SubscriberManagerPanel;
import com.library.admin.service.LibraryService;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

public class FindSubscriberByLoginListener implements ClickListener {
	private static final long serialVersionUID = -5237054459032685599L;

	private SubscriberManagerPanel subscriberManagerPanel;
	
	public FindSubscriberByLoginListener(SubscriberManagerPanel subscriberManagerPanel) {
		this.subscriberManagerPanel = subscriberManagerPanel;
	}

	public void buttonClick(ClickEvent event) {
		AdmLibrary libraryApplication = (AdmLibrary) subscriberManagerPanel.getApplication();
		String subscriberLogin = subscriberManagerPanel.getSubscriberLogin().getValue().toString();
		
		if (subscriberManagerPanel.getComponentCount() == 3) {
			subscriberManagerPanel.removeComponent(subscriberManagerPanel.getComponent(2));
		}
		try {
			SubscriberEntity subscriber = LibraryService.getServiceSubscriber().findSubscriberByLogin(subscriberLogin);
			subscriberManagerPanel.addComponent(new UpdateSubscriberAccountForm(subscriber));
		}
		catch (Exception e) {
			libraryApplication.getMainWindow().showNotification("Aucun abonné trouvé avec login '" + subscriberLogin + "'",
					Notification.TYPE_ERROR_MESSAGE);
			e.printStackTrace();
		}
	}
}
